package com.example.controller;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.annotation.Log;
import com.example.common.result.Result;
import com.example.entity.yifei.Invtj;
import com.example.entity.yifei.Invtk;
import com.example.mapper.InvtjMapper;
import com.example.mapper.InvtkMapper;
import com.example.service.InvtjService;
import com.example.service.impl.ActFlowCommService;
import com.example.untils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/invmj")
@Api(tags = "存货调整单前端控制器")
public class InvtjController {

    @Resource
    private ActFlowCommService actFlowCommService;

    @Resource
    private InvtjService invtjService;

    @Resource
    private InvtjMapper invtjMapper;

    @Resource
    private InvtkMapper invtkMapper;

    @Log("上报存货调整单")
    @ApiOperation("上报存货调整单")
    @GetMapping("/report")
    @DSTransactional
    public Result report(
            @RequestParam String TC001, @RequestParam String TC002, @RequestParam String username, @RequestParam String leader
    ) {
        if (StringUtils.isEmpty(TC001) || StringUtils.isEmpty(TC002)) {
            return Result.error("单别或单号为空");
        }
        if (StringUtils.isEmpty(username)) {
            return Result.error("用户名为空");
        }
        String formkey = "invtj";
        String beanName = formkey + "ServiceImpl";
        ProcessInstance processInstance;
        String bussinessKey = formkey + ":" + TC001 + ":" + TC002;
        String id = TC001 + ":" + TC002;
        Boolean b = actFlowCommService.isOnly(bussinessKey, formkey);
        Boolean b2 = invtjService.tc30IsN(TC001, TC002);
        if (b && b2) {
            processInstance = actFlowCommService.startProcess(formkey, beanName, bussinessKey, id, username, leader);
        } else {
            return Result.error("有相同单据正在审核或该单据已审核完成");
        }
        //获取流程实例id
        String processInstanceId = processInstance.getId();
        List<Task> taskList = actFlowCommService.myTaskList(username);
        if (!CollectionUtils.isEmpty(taskList)) {
            for (Task task : taskList) {
                if (task.getAssignee().equals(username) &&
                        task.getProcessInstanceId().equals(processInstanceId)) {
                    log.info("ProcessInstanceId is {}", task.getProcessInstanceId());
                    log.info("taskid is {}",task.getId());
                    actFlowCommService.completeProcess("同意", username, task.getId(),"");
                }
            }
        }
        else {
            return Result.error("空任务！");
        }
        return Result.success("上报存货调整单成功");
    }

    @Log("存货调整单详情")
    @ApiOperation("存货调整单详情  invtj 存货调整单单头 invtk 存货调整单单身")
    @PostMapping("/queryXq")
    public Result<Map<String,Object>> queryXq(@RequestParam(defaultValue = "") String TC001,
                                              @RequestParam(defaultValue = "") String TC002){

        if (StringUtils.isEmpty(TC001) || StringUtils.isEmpty(TC002)) {
            return Result.error("单别或单号为空");
        }
        Invtj invtj = invtjMapper.selectOne(new LambdaQueryWrapper<Invtj>().eq(Invtj::getTJ001, TC001).eq(Invtj::getTJ002, TC002));
        if (invtj == null) {
            return Result.error("存货调整单不存在");
        }
        List<Invtk> invtkList = this.invtkMapper.selectList(new LambdaQueryWrapper<Invtk>().eq(Invtk::getTK001, TC001).eq(Invtk::getTK002, TC002));
        Map<String,Object> map = new HashMap<>();
        map.put("invtj",invtj);
        map.put("invtkList",invtkList);
        return Result.success(map);
    }
}
